package com.chen.util;

import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import org.json.JSONException;
import java.io.IOException;

/**
 * @author JohnSong
 * @date 2019/9/24 22:06
 **/
public class PhoneUtil {
    private static final int appid=1400256773;
    private static final String appkey = "4cddd2ea3543dc195eeed6f3cd7e8cde";
    private static final String smsSign = "山财开源社团";
    //短信模板ID，可根据实际情况进行更改
    private static final int templateId = 422204;

    public static boolean sendCode(String to, String code) throws Exception {
        try {
            //该模板对应的{ }内容设置
            String[] params = {code};
            SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
            SmsSingleSenderResult result = ssender.sendWithParam("86", to,
                    templateId, params, smsSign, "", "");
            System.out.println(result);
        } catch (HTTPException e) {
            e.printStackTrace();
            return false;
        } catch (JSONException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
